<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form size="small" inline label-width="100px">
          <el-form-item
            label="姓名"
            class="width100"
            style="display: inline-block;"
          >
            <el-input
              v-model="tableFrom.real_name"
              placeholder="请输入姓名"
              class="selWidth"
              size="small"
              @keyup.enter.native="getList(1)"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                size="small"
                class="el-button-solt"
                @click="getList(1)"
              />
            </el-input>
          </el-form-item>
          <el-form-item label="所在区县" prop="city">
            <el-cascader
              style="display: block;"
              v-model="tableFrom.city"
              :props="cityProps"
              :options="cityList"
              :placeholder="'请选择'"
              @change="handleCascaderChange"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item
            label="所属公司"
            class="width100"
            style="display: inline-block;"
          >
            <el-cascader
              style="display: block;"
              v-model="tableFrom.superior_cid"
              :props="companyProps"
              :options="companyList"
              :placeholder="'请选择'"
              @change="getList(1)"
              clearable=""
            ></el-cascader>
          </el-form-item>
          <el-button size="small" type="primary" @click="resetForm"
            >重置</el-button
          >
        </el-form>
      </div>
      <el-table
        v-loading="sall.loading"
        :data="sall.data"
        style="width: 100%"
        size="mini"
        name="m1"
        :row-key="
          row => {
            return row.id;
          }
        "
      >
        <el-table-column prop="ranking" label="总排名">
          <template slot-scope="{ $index }">
            {{ tableFrom.limit * (tableFrom.page - 1) + $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="real_name" label="员工姓名" />
        <el-table-column prop="status" label="状态">
          <template
            >已加入</template
          >
        </el-table-column>
        <el-table-column prop="name" label="所属公司" />
        <el-table-column label="上级公司">
          <template slot-scope="scope">{{ scope.row.superior.name }}</template>
        </el-table-column>
        <el-table-column prop="area_name" label="所在区域" />
        <el-table-column prop="team_count" label="推广人数" />
        <el-table-column prop="sale_count" label="办卡数量" />
        <el-table-column prop="sale_open_count" label="激活数量" />
        <el-table-column prop="sale_save_count" label="首充数量" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-link type="primary" @click="onDetails(scope.row)"
              >查看业绩</el-link
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="sall.total"
          @size-change="sizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { KsCity } from "@/api/staff.js";
import { roterPre } from "@/settings";
import { companySubList } from "@/api/company.js";
import { staffRank } from "@/api/statistics.js";
export default {
  name: "",
  data() {
    return {
      sall: {
        list: [],
        total: 0,
        loading: false
      },
      tableFrom: {
        page: 1,
        limit: 20,
        real_name: "",
        superior_cid: "",
        province_id: "",
        city_id: "",
        district_id: "",
        type: 1
      },
      cityList: [],
      cityProps: {
        lazy: true,
        label: "name",
        value: "id",
        that: this,
        checkStrictly: true,
        lazyLoad(node, resolve) {
          const { level, data } = node;
          KsCity(data ? data.id : 0)
            .then(res => {
              const nodes = res.data.city.map(item => ({
                id: item.id,
                name: item.area_name,
                leaf: level >= 2
              }));
              resolve(nodes);
            })

            .catch(err => {
              this.$message.error(err.message);
            });
        }
      },
      companyList: [],
      companyProps: {
        lazy: true,
        label: "name",
        value: "id",
        that: this,
        checkStrictly: true,
        emitPath: false,
        lazyLoad(node, resolve) {
          const { level, data } = node;
          companySubList(data ? data.id : 0)
            .then(res => {
              const nodes = res.data.list.map(item => {
                let limit = 1;
                if (this.that.adminInfo.company_type == 2) {
                  limit = 1;
                } else if (
                  this.that.adminInfo.company_type == 0 ||
                  this.that.adminInfo.company_type == 1
                ) {
                  limit = 1;
                } else if (this.that.adminInfo.company_type == 3) {
                  limit = 0;
                }

                return {
                  id: item.id,
                  name: item.name,
                  leaf: level >= limit
                };
              });
              resolve(nodes);
            })

            .catch(err => {
              this.$message.error(err.message);
            });
        }
      },
      adminInfo: {}
    };
  },
  watch: {},
  computed: {},
  methods: {
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList(1);
    },
    sizeChange(val) {
      this.tableFrom.limit = val;
      this.getList(1);
    },
    getList(page) {
      this.sall.loading = true;
      this.tableFrom.page = page ? page : this.tableFrom.page;
      const param = JSON.parse(JSON.stringify(this.tableFrom));
      delete param.city;
      staffRank(param)
        .then(res => {
          this.sall.loading = false;
          this.sall.data = res.data.list;
          this.sall.total = res.data.count;
        })
        .catch(e => {
          this.$message.error(e);
        });
    },
    onDetails(row) {
      this.$router.push({
        path: roterPre + "/staff/staffDetail",
        query: { id: row.card_code }
      });
    },
    resetForm() {
      this.tableFrom.city = "";
      this.tableFrom.real_name = "";
      this.tableFrom.page = 1;
      this.tableFrom.superior_cid = "";
      this.tableFrom.company = "";
      this.tableFrom.province_id = "";
      this.tableFrom.city_id = "";
      this.tableFrom.district_id = "";
      this.getList(1);
    },
    handleCascaderChange(value) {
      // 处理级联选择器值改变事件
      if (value && value.length > 0) {
        switch (value.length) {
          case 1:
            this.tableFrom.province_id = value[0];
            this.tableFrom.city_id = "";
            this.tableFrom.district_id = "";
            break;
          case 2:
            this.tableFrom.province_id = value[0];
            this.tableFrom.city_id = value[1];
            this.tableFrom.district_id = "";
            break;
          case 3:
            this.tableFrom.province_id = value[0];
            this.tableFrom.city_id = value[1];
            this.tableFrom.district_id = value[2];
            break;
        }
      } else {
        this.tableFrom.province_id = "";
        this.tableFrom.city_id = "";
        this.tableFrom.district_id = "";
      }
      this.getList(1);
    },
    //获取子公司列表
    getCompanySubList() {
      companySubList().then(res => {
        this.companyList = res.data.list;
      });
    }
  },
  created() {},
  mounted() {
    const adminInfo = Cookies.get("AdminInfo");
    this.$set(this, "adminInfo", JSON.parse(adminInfo));
    this.getList(1);
  }
};
</script>

<style lang="scss" scoped>
.header {
  display: flex;
  width: 100%;
  height: 61px;
  align-items: center;
  line-height: 24px;
  padding-left: 27px;
  font-size: 18px;
  color: #333333;
  border-bottom: 1px solid #eeeeee;

  .dot {
    width: 10px;
    height: 10px;
    border-radius: 5px;
    margin-right: 4px;
    background: #385be1;
  }
}
.el-table {
  min-height: 300px;
}
</style>
